package com.gxa.controller.cm;

import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.param.cm.QueryParam;
import com.gxa.pojo.cm.Devplan;
import com.gxa.pojo.cm.vo.TableVO;
import com.gxa.service.cm.DevplanService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author Glenn
 */
@Controller
@RequestMapping("/cm/devplan")
public class DevplanController {

    @Resource
    DevplanService devplanService;

    @GetMapping("/list/page")
    public String list() {
        return "cm/devplan-list";
    }

    @PostMapping("/list/data")
    @ResponseBody
    public TableVO listData(QueryParam queryParam, @RequestParam(value = "selectedMonths[]", required = false) String[] selectedMonths) {
        if (selectedMonths != null && selectedMonths.length > 0) {
            queryParam.setMonths(Arrays.asList(selectedMonths));
        }
        PageInfo<Devplan> devplanPageInfo = devplanService.listDevplan(queryParam);
        TableVO tableVO = new TableVO();
        tableVO.setCode(0)
                .setMsg("成功")
                .setCount(devplanPageInfo.getTotal())
                .setData(devplanPageInfo.getList());
        return tableVO;
    }

    @GetMapping("/add/page")
    public String add() {
        return "cm/devplan-add";
    }

    @PostMapping("/add/data")
    @ResponseBody
    public TableVO addData(Devplan devplan, String name, HttpSession session) {
        return devplanService.addDevplan(devplan, name, session);
    }

    @GetMapping("/edit/page")
    public String edit(Integer id, Model model) {
        Devplan devplan = devplanService.getById(id);
        model.addAttribute("devplan", devplan);
        return "cm/devplan-edit";
    }

    @GetMapping("/detail/page")
    public String detail(Integer id, Model model) {
        Devplan devplan = devplanService.getById(id);
        model.addAttribute("devplan", devplan);
        return "cm/devplan-detail";
    }

    @PostMapping("/updateDevplan")
    @ResponseBody
    public TableVO updateCustomer(Devplan devplan, String name, HttpSession session) {
        return devplanService.updateDevplan(devplan, name, session);
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    public R batchDelete(@RequestParam("ids[]") Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        return devplanService.batchDelete(idList);
    }

}
